/* 
 * Document   : cubemarketing-script
 * Created on : 15 mai 2013, 09:23:43
 * Author     : Mamy RAKOTONIRINA
 * Description: JS for Cubemarketing
 */


$(document).ready(function() {
    var navIdx = 100;
    var uri = document.location.pathname; 
    var icons = {
        header: "ui-icon-carat-1-e",
        activeHeader: "ui-icon-carat-1-s"
    };
    
    if(uri.match(/produit/)){
        navIdx = 1;
    } else if(uri.match(/famille/) || uri.match(/gamme/) || uri.match(/allergene/) || uri.match(/production/) || uri.match(/plateau/) || uri.match(/fournisseur/) || uri.match(/tva/) || uri.match(/dlc/) || uri.match(/service/) || uri.match(/contenant/) || uri.match(/ingredient/) || uri.match(/bio/)){
        navIdx = 0;
    }
    
    $( "#nav-cube" ).accordion({
        icons: icons,
        active: navIdx
    });
    
    $(".datepicker").datepicker({
        maxDate: '+0D', 
        dateFormat: "yy-mm-dd",
        dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'], 
        dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'], 
        monthNamesShort: ['Jan','Fev','Mar','Avr','Mai','Jun','Jul','Août','Sep','Oct','Nov','Déc'], 
        monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'], 
        prevText: 'Précédent', 
        nextText: 'Suivant'	
    });
    
    $("#startDate").datepicker({
        maxDate: '+0D',
        dateFormat: 'dd/mm/yy', 
        altFormat: "yy-mm-dd",
        altField: '#altStartDate',
        dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'], 
        dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'], 
        monthNamesShort: ['Jan','Fev','Mar','Avr','Mai','Jun','Jul','Août','Sep','Oct','Nov','Déc'], 
        monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'], 
        prevText: 'Précédent', 
        nextText: 'Suivant'
    });
    
    $("#endDate").datepicker({
        maxDate: '+0D',
        dateFormat: 'dd/mm/yy', 
        altFormat: "yy-mm-dd",
        altField: '#altEndDate',
        dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'], 
        dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'], 
        monthNamesShort: ['Jan','Fev','Mar','Avr','Mai','Jun','Jul','Août','Sep','Oct','Nov','Déc'], 
        monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'], 
        prevText: 'Précédent', 
        nextText: 'Suivant'
    });
});

$.event.special.hoverintent = {
    setup: function() {
        $( this ).bind( "mouseover", jQuery.event.special.hoverintent.handler );
    },
    teardown: function() {
        $( this ).unbind( "mouseover", jQuery.event.special.hoverintent.handler );
    },
    handler: function( event ) {
        var currentX, currentY, timeout,
            args = arguments,
            target = $( event.target ),
            previousX = event.pageX,
            previousY = event.pageY;
    
        function track( event ) {
            currentX = event.pageX;
            currentY = event.pageY;
        };
        
        function clear() {
            target
                .unbind( "mousemove", track )
                .unbind( "mouseout", clear );
            clearTimeout( timeout );
        }
        function handler() {
            var prop,
                orig = event;
        
            if ( ( Math.abs( previousX - currentX ) +
            Math.abs( previousY - currentY ) ) < 7 ) {
                clear();
                event = $.Event( "hoverintent" );
                for ( prop in orig ) {
                    if ( !( prop in event ) ) {
                        event[ prop ] = orig[ prop ];
                    }
                }
                delete event.originalEvent;
                target.trigger( event );
            } else {
                previousX = currentX;
                previousY = currentY;
                timeout = setTimeout( handler, 100 );
            }
        }
        timeout = setTimeout( handler, 100 );
        target.bind({
        mousemove: track,
        mouseout: clear
        });
    }
};

function listbox_selectall(listID, isSelect) {
    var listbox = document.getElementById(listID);
    for(var count=0; count < listbox.options.length; count++) {
        listbox.options[count].selected = isSelect;
    }
}

function listbox_moveacross(sourceID, destID) {
    var src = document.getElementById(sourceID);
    var dest = document.getElementById(destID);
    
    for(var count=0; count < src.options.length; count++) {
        if(src.options[count].selected == true) {
            var option = src.options[count];
            var newOption = document.createElement("option");
            newOption.value = option.value;
            newOption.text = option.text;
            newOption.selected = true;
            try {
                dest.add( newOption, null); //Standard
                src.remove(count, null);
            }catch(error) {
                dest.add( newOption); //Standard
                //dest.add(newOption); // IE only
                src.remove(count);
            }
            count--;
        }
    }
}

function sortlist(mylist) {
    var lb = document.getElementById(mylist);
    var arrTexts = new Array();
    
    for(i=0; i<lb.length; i++)  {
        arrTexts[i].text = lb.options[i].text;
        arrTexts[i].value = lb.options[i].value;
    }
    
    arrTexts.sort();
    
    for(i=0; i<lb.length; i++)  {
        lb.options[i].text = arrTexts[i].text;
        lb.options[i].value = arrTexts[i].value;
    }
}

function dateToYMD(date) {
    var d = date.getDate();
    var m = date.getMonth() + 1;
    var y = date.getFullYear();
    return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
}

function dateToDMY(date) {
    var d = date.getDate();
    var m = date.getMonth() + 1;
    var y = date.getFullYear();
    return '' + (d <= 9 ? '0' + d : d) + '/' + (m<=9 ? '0' + m : m) + '/' + y;
}


function cDelete(id){
    // Manage Uri
    var uri = document.location.pathname;
    
    if(confirm('Voulez vous vraiment supprimé?')){
        window.location.href = uri+'/delete/'+id;
    }
}